<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li>
                  <a title="全部" href="javascript:void(0)" @click="whole">全部</a>
                </li>
                <li v-for="(item,index) in SubjectOnelist" :key="index">
                  <a :title="item.label" href="javascript:void(0)" @click="SubjectOne(item)">{{item.label}}</a>
                </li>

              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"></span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix" v-if="Subjecttwolist.length>0">
                <li v-for="(item,index) in Subjecttwolist" :key="index">
                  <a :title="item.label" href="javascript:void(0)" @click="SubjectTwo(item)">{{item.label}}</a>
                </li>

              </ul>
            </dd>
          </dl>
          <div class="clear"></div>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <section class="fl">
            <ol class="js-tap clearfix">
              <li :class="{'current bg-orange':sorts==0}" @click="Coursesort(0)">
                <a title="关注度" href="javascript:void(0)">关注度</a>
              </li>
              <li :class="{'current bg-orange':sorts==1}" @click="Coursesort(1)">
                <a title="最新" href="javascript:void(0)">最新</a>
              </li>
              <li :class="{'current bg-orange':sorts==2}" @click="Coursesort(2)">
                <a title="价格" href="javascript:void(0)">价格&nbsp;
                  <span>↓</span>
                </a>
              </li>
            </ol>
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap" v-if="Courselist.total==0">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam">没有相关数据，小编正在努力整理中...</span>
          </section>
          <!-- /无数据提示 结束-->
          <article class="comm-course-list">
            <ul class="of" id="bna">
              <li v-for="(item,index) in Courselist.items" :key="index">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <router-link :to="/course/+item.id">
                        <img
                          v-if="isImg(item.cover)"
                          src="@/static/timg.jpg"
                          class="img-responsive "
                          style="width: 257.5px;height: 143.6px;"
                          :alt="item.title"
                        >
                        <img
                          v-else
                          :src="item.cover"
                          class="img-responsive "
                          style="width: 257.5px;height: 143.6px;"
                          :alt="item.title"
                        >
                    </router-link>
                    <div class="cc-mask">
                      <a :href="/course/+item.id" title="开始学习" class="comm-btn c-btn-1">开始学习</a>
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a href="/course/1" title="听力口语" class="course-title fsize18 c-333">听力口语</a>
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span class="fr jgTag bg-green" :style="{background:(item.price!=0?'#fff':'')}">

                      <i class="c-fff fsize12 f-fA" :style="{color:(item.price!=0?'red':''),fontSize:(item.price!=0?'18px':''),fontWeight:(item.price!=0?'bold':'')}" >{{item.price==0?"免 费":"￥"+item.price}}</i>

                    </span>
                      <span class="fl jgAttr c-ccc f-fA">

                        <i class="c-999 f-fA">{{item.viewCount}}浏览</i>

                        |

                        <i class="c-999 f-fA">{{item.buyCount}}销售</i>

                      </span>

                  </section>
                </div>
              </li>

            </ul>
          </article>
        </div>
        <!-- 公共分页 开始 -->
        <div>
           <div class="paging">
             <!-- undisable这个class是否存在，取决于数据属性hasPrevious -->
             <a
               :class="{undisable: !Courselist.hasPrevious}"
               href="#"
               title="首页"
               @click.prevent="pageCourselist(1)">首</a>
             <a
               :class="{undisable: !Courselist.hasPrevious}"
               href="#"
               title="前一页"
               @click.prevent="pageCourselist(Courselist.current-1)">&lt;</a>
             <a
               v-for="page in Courselist.pages"
               :key="page"
               :class="{current: Courselist.current == page, undisable: Courselist.current == page}"
               :title="'第'+page+'页'"
               href="#"
               @click.prevent="pageCourselist(page)">{{ page }}</a>
             <a
               :class="{undisable: !Courselist.hasNext}"
               href="#"
               title="后一页"
               @click.prevent="pageCourselist(Courselist.current+1)">&gt;</a>
             <a
               :class="{undisable: !Courselist.hasNext}"
               href="#"
               title="末页"
               @click.prevent="pageCourselist(Courselist.pages)">末</a>

         </div>
        </div>
        <!-- 公共分页 结束 -->
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>
<script>
  import {pageCourse,getAllSubject} from '~/http'
export default {
  data(){
    return {
      page :1,
      Courselist:[],//所有课程
      CourseFrontVo:{},//筛选条件
      SubjectOnelist:[],//头类目
      Subjecttwolist:[],//子类目
      sorts:0,
    }
  },
  created() {
    this.getAll()
    this.pageCourselist()
  },
  methods:{
    getAll(){
      //获取类目
      getAllSubject().then(res=>{
        console.log(res.data.data1)
        this.SubjectOnelist = res.data.data1
      })
    },
    //获取课程
    pageCourselist(page = 1){
      this.page = page;
      pageCourse(page,8,this.CourseFrontVo).then(res=>{
        this.Courselist = res.data
      })
    },
    // 图片判断
    isImg(text){
      if(text.includes("https"))return false
       return true
    },


    //全部数据
    whole(){
      this.CourseFrontVo = {}
      this.Subjecttwolist = []
      this.pageCourselist()
    },
    //筛选类目
    SubjectOne(item){
      this.CourseFrontVo.subjectId = ""
      this.CourseFrontVo.subjectParentId = item.id
      this.Subjecttwolist = item.children
      this.pageCourselist()
    },
    //筛选子类目
    SubjectTwo(item){
      this.CourseFrontVo.subjectId = item.id
      this.pageCourselist()
    },
    //选择关注度和价格排序
    Coursesort(e){
      this.CourseFrontVo.buyCountSort = this.CourseFrontVo.gmtCreateSort = this.CourseFrontVo.priceSort = ''
      this.sorts = e
      switch(e) {
           case 0:
              this.CourseFrontVo.buyCountSort = "buyCountSort"
              break;
           case 1:
              this.CourseFrontVo.gmtCreateSort = "gmtCreateSort"
              break;
           case 2:
              this.CourseFrontVo.priceSort = "priceSort"
              break;
           default:
             this.pageCourselist()
      }
      this.pageCourselist()
    }

  }

 };
</script>
